const { logsModel } = require('./models/logsModel');
const { adminsModel } = require('./models/adminsModel');
// const mongoose = require('mongoose');

module.exports.getLogs = async function() {
    const result = await logsModel.find().populate('adminsId').limit(10);
    return result;
}

module.exports.searchLogs = async function(data) {
    const reg = new RegExp(data, 'i'); //不区分大小写
    const result = await adminsModel.find({
        $or: [{ account: { $regex: reg } }]
    });
    // const result1 = await logsModel.find().populate('adminsId');
    let resultAll = [];
    for (let i = 0; i < result.length; i++) {
        const result1 = await logsModel.find({ adminsId: result[i]._id }).populate('adminsId').limit(10);
        resultAll = resultAll.concat(result1);
    }
    return resultAll;
}

module.exports.logsDelete = async function(data) {
    await logsModel.findByIdAndDelete(data);
}